/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.pneuservis.soap.sei.impl;

import cz.muni.fi.pa165.pneuservis.soap.dto.CustomerDTO;
import cz.muni.fi.pa165.pneuservis.soap.dto.Transfer;
import cz.muni.fi.pa165.pneuservis.soap.sei.CustomerSEI;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jws.WebParam;
import javax.jws.WebService;
import pneuservis.backend.services.CustomerServiceLocal;
import pneuservis.backend.to.CustomerTO;

@WebService(endpointInterface="cz.muni.fi.pa165.pneuservis.soap.sei.CustomerSEI", serviceName="CustomerService")
@Stateless
public class CustomerService implements CustomerSEI {

    @EJB(lookup="java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/CustomerService")
    CustomerServiceLocal customerService;
    
    @Override
    public void createCustomer(@WebParam(name = "customer") CustomerDTO customer) {
        customerService.createCustomer(Transfer.toCustomerTO(customer));
    }

    @Override
    public void updateCustomer(@WebParam(name = "customer") CustomerDTO customer) {
        customerService.updateCustomer(Transfer.toCustomerTO(customer));
    }

    @Override
    public void removeCustomer(@WebParam(name = "customer") CustomerDTO customer) {
        customerService.removeCustomer(Transfer.toCustomerTO(customer));
    }

    @Override
    public CustomerDTO getCustomer(@WebParam(name = "id") Long id) {
        return Transfer.toCustomerDTO(customerService.getCustomer(id));
    }

    @Override
    public CustomerDTO[] getAllCustomers() {
        List<CustomerTO> allCustomers = customerService.getAllCustomers();
        return asCustomerArray(allCustomers);
    }

    @Override
    public CustomerDTO[] findCustomerByName(String name){
        return asCustomerArray(customerService.findCustomerByName(name));
    }
    
    private CustomerDTO[] asCustomerArray(List<CustomerTO> allCustomers) {
        List<CustomerDTO> result = new ArrayList<>();
        for(CustomerTO c : allCustomers){
            result.add(Transfer.toCustomerDTO(c));
        }
        CustomerDTO[] a = new CustomerDTO[0];
        return result.toArray(a);
    }
}
